﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.UI;
using System.Diagnostics;

namespace CCD
{
    public partial class ModelAndBlockManager : Form
    {
        public ModelAndBlockManager()
        {
            InitializeComponent();
            GetModelInfo();
        }

        private void GetModelInfo()
        {
            //Load Models
            DirectoryInfo di = new DirectoryInfo(Paths.ModelPath);
            try
            {
                //Clear the box first
                listBox1.BeginUpdate();
                listBox1.Items.Clear();
                listBox1.EndUpdate();
                listBox2.BeginUpdate();
                listBox2.Items.Clear();
                listBox2.EndUpdate();

                // Determine whether the directory exists.
                if (di.Exists)
                {
                    DirectoryInfo[] DirList = di.GetDirectories();

                    listBox1.BeginUpdate();
                    for (int i = 0; i < DirList.Length; i++)
                    {
                        listBox1.Items.Add(DirList[i].Name);
                    }
                    listBox1.EndUpdate();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The model directory loading process failed: {0}", e.ToString());
            }
            finally { }

            di = null;
        }

        private void GetBlockInfo()
        {
            if (listBox1.SelectedItem != null)
            {
                DirectoryInfo di = new DirectoryInfo(Paths.ModelPath + "\\" + listBox1.SelectedItem.ToString() + "\\blocks");
                try
                {
                    //Clear the box first
                    listBox2.BeginUpdate();
                    listBox2.Items.Clear();
                    listBox2.EndUpdate();

                    // Determine whether the directory exists.
                    if (di.Exists)
                    {
                        DirectoryInfo[] DirList = di.GetDirectories();

                        listBox2.BeginUpdate();
                        for (int i = 0; i < DirList.Length; i++)
                        {
                            listBox2.Items.Add(DirList[i].Name);
                        }
                        listBox2.EndUpdate();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The block directory loading process failed: {0}", e.ToString());
                }
                finally { }

                di = null;
            }
        }

        public bool AddModel(string Model)
        {
            if (listBox1.FindString(Model) != -1)
                return false;
            else
            {
                DirectoryInfo di = new DirectoryInfo(Paths.ModelPath + "\\" + Model + "\\blocks");
                di.Create();

                GetModelInfo();

                di = null;

                return true;
            }
        }

        public void AddBlock(string ModelName, string BlockName)
        {
            if (listBox2.FindString(BlockName) != -1)
                MessageBox.Show("This name has already existed! Try another!");
            else
            {
                DirectoryInfo di = new DirectoryInfo(Paths.ModelPath + "\\" + ModelName + "\\blocks\\" + BlockName);
                di.Create();

                GetBlockInfo();

                Image<Rgb, Byte> Img = new Image<Rgb, Byte>(Paths.ModelPath + "\\" + ModelName + "\\imgAve.bmp");
                BlockDefine son = new BlockDefine(BlockName, Paths.ModelPath + "\\" + ModelName + "\\blocks\\" + BlockName, Img);
                son.ShowDialog();
            }
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string curItem = listBox1.SelectedItem.ToString();
            string BlockPath = Paths.ModelPath + "\\" + curItem + "\\blocks";

            DirectoryInfo di = new DirectoryInfo(BlockPath);
            try
            {
                //Clear the box first
                listBox2.BeginUpdate();
                listBox2.Items.Clear();
                listBox2.EndUpdate();

                // Determine whether the directory exists.
                if (di.Exists)
                {
                    DirectoryInfo[] DirList = di.GetDirectories();

                    listBox2.BeginUpdate();
                    for (int i = 0; i < DirList.Length; i++)
                    {
                        listBox2.Items.Add(DirList[i].Name);
                    }
                    listBox2.EndUpdate();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The block directory loading process failed: {0}", ex.ToString());
            }
            finally { }

            di = null;
        }

        private void AddModel_Click(object sender, EventArgs e)
        {
            AddModelDialog Son = new AddModelDialog();
            Son.Owner = this;
            Son.ShowDialog();
        }

        private void DeleteModel_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
                MessageBox.Show("You havn't selected any model!");
            else
            {
                if (MessageBox.Show("Are you sure to delete?", "CONTINUE?", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DirectoryInfo di = new DirectoryInfo(Paths.ModelPath + "\\" + listBox1.SelectedItem.ToString());

                    di.Delete(true);

                    GetModelInfo();
                }
            }  
        }

        private void AddBlock_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            {
                MessageBox.Show("You don't choose any model!");
            }
            else if (File.Exists(Paths.ModelPath + "\\" + listBox1.SelectedItem.ToString() + "\\imgAve.bmp"))
            {
                //Image<Rgb, Byte> img = new Image<Rgb, Byte>(Paths.ModelPath + "\\" + listBox1.SelectedItem.ToString() + "\\imgAve.bmp");

                AddBlockDialog son = new AddBlockDialog(listBox1.SelectedItem.ToString());
                son.Owner = this;
                son.ShowDialog();
            }
            else
            {
                MessageBox.Show("Model data don't exist!");
            }
        }

        private void DeleteBlock_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null || listBox2.SelectedItem == null)
                MessageBox.Show("You havn't selected any block!");
            else
            {
                if (MessageBox.Show("Are you sure to delete?", "CONTINUE?", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DirectoryInfo di = new DirectoryInfo(Paths.ModelPath + "\\" + listBox1.SelectedItem.ToString() + "\\blocks\\" + listBox2.SelectedItem.ToString());

                    di.Delete(true);

                    GetBlockInfo();
                }
            }
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            this.Owner.Show();
        }
    }

    public static class Paths
    {
        public static string ModelPath = "data\\model";
        public static string GetModelPath(string modelName)
        {
            return Path.Combine( ModelPath, modelName );
        }
        public static string GetBlockPath(string modelName, string blockName )
        {
            return Path.Combine(ModelPath, modelName, "blocks", blockName);
        }
        public static string GetMaskFilename(string modelName, string blockName)
        {
            return Path.Combine(new string[] { ModelPath, modelName, "blocks", blockName, "mask.bmp" });
        }
    }   
}
